<template>
	<view>
		<!-- 地址 -->
		<!-- <view class="navbar">
			<view v-for="(item, index) in navList" :key="index" class="nav-item" :class="{current: tabCurrentIndex === index}"
			 @click="tabClick(index)">
				{{item.text}}
			</view>
		</view> -->
		<navigator v-if="tabCurrentIndex==0" url="/pages/address/address?source=1" class="address-section">
			<view class="order-content" v-if="addressData.name">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen">
					<view class="top">
						<text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.telephone}}</text>
					</view>
					<text class="address">{{addressData.province}}{{addressData.city}}{{addressData.area}}{{addressData.street}}</text>
				</view>
				<text class="yticon icon-you"></text>
			</view>
			<view class="order-content" v-if="!addressData.name">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen">
					<view class="top">
						<!-- <text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.telephone}}</text> -->
						<text>请选择地址</text>
					</view>
					<!-- <text class="address">{{addressData.street}}</text> -->
				</view>
				<text class="yticon icon-you"></text>
			</view>
			<image class="a-bg" src=""></image>
		</navigator>
		<navigator v-if="tabCurrentIndex==1" url="/pages/merchant/list" class="address-section">
			<view class="order-content" v-if="merchantData.merchantUuid">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen">
					<view class="top">
						<text class="name">{{merchantData.merchantName}}</text>
					</view>
					<text class="address">{{merchantData.merchantAddress}}公里</text>
				</view>
				<text class="yticon icon-you"></text>
			</view>
			<view class="order-content" v-if="!merchantData.merchantUuid">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen">
					<view class="top">
						<!-- <text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.telephone}}</text> -->
						<text>请选择门店</text>
					</view>
					<!-- <text class="address">{{addressData.street}}</text> -->
				</view>
				<text class="yticon icon-you"></text>
			</view>
		</navigator>

		<view class="goods-section">
			<!-- 			<view class="g-header b-b">
				<image class="logo" src="http://duoduo.qibukj.cn/./Upload/Images/20190321/201903211727515.png"></image>
				<text class="name">西城小店铺</text>
			</view> -->
			<!-- 商品列表 -->
			<view class="g-item">
				<image :src="product.productMainImage.url"></image>
				<view class="right">
					<text class="title clamp">{{product.productName}}</text>
					<text class="spec" v-if="product.skuEnabled">
						<text v-for="sku in productSku.skuAttrValueList">{{sku.skuAttrValue}}</text>
					</text>
					<text class="spec" v-if="!product.skuEnabled">-</text>
					<view class="price-box">
						<text class="price">￥{{groupbuy.unitPrice}}</text>
						<text class="number">x {{unit}}</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">商品金额</text>
				<text class="cell-tip red">￥{{productAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">优惠金额</text>
				<text class="cell-tip">￥{{deductAmount}}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip" v-if="tabCurrentIndex==0">￥{{freightAmount}}</text>
				<text class="cell-tip" v-if="tabCurrentIndex==1">￥0</text>
			</view>
			<view class="yt-list-cell desc-cell">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="memo" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{actualAmount}}</text>
			</view>
			<text class="submit" @click="submit">提交订单</text>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import uniPopup from "@/components/uni-popup/uni-popup.vue"
	export default {
		data() {
			return {
				tabCurrentIndex:0,
				navList: [{
						state: '1',
						text: '快递配送'
					}],
				quanlificationId: '',
				groupBuyProductId: '',
				groupBuyId:'',
				groupbuy:{
					unitPrice: 0.00
				},
				productId: '',
				product:{},
				productSkuId: '',
				productSku: {},
				productAmount: 0.00, //商品金额, 未扣减任何优惠
				deductAmount: 0.00, //优惠金额
				actualAmount: 0.00, //实付金额
				freightAmount: 0.00, //运费
				unit: '',
				addressData: {}, //收货地址
				merchantData: {}, //门店地址
				memo: '', //备注
				type: ''
			}
		},
		components: {
			uniPopup
		},
		onLoad(option) {
			//商品数据
			this.quanlificationId = option.quanId;
			this.groupBuyProductId = option.groupBuyProductId;
			this.groupBuyId = option.groupBuyId;
			this.productId = option.productId;
			this.productSkuId = option.productSkuId;
			this.unit = option.unit
			//如果开启了门店
			if(this.applicationConfig.applicationMerchantEnabled){
				this.navList.push({
						state: '2',
						text: '门店自提'
					})
			}
			this.inquiryGroupBuyById(this.groupBuyProductId);
			this.inquiryDefaultAddress(this.userInfo.userUuid);
		},
		computed: {
			...mapState(['hasLogin', 'userInfo', 'applicationConfig'])
		},
		watch: {
			addressData(val) {
				this.calculateFreightAmount(this.groupBuyProductId, val.province, val.city, val.area);
			}
		},
		methods: {
			toActualAmount(productAmount,deductAmount,freightAmount){
				return (productAmount - deductAmount + freightAmount).toFixed(2);
			},
			tabClick(index) {
				this.tabCurrentIndex = index;
				if(index==1){
					this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,0);
				}else if(index==0){
					this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,this.freightAmount);
				}
			},
			//查询默认收货地址
			inquiryDefaultAddress(userUuid) {
				let that = this;
				let postData = {
					userDTO: {
						userUuid: userUuid
					}
				}
				this.$api.request.userShip(postData, res => {
					if (res.body.status.statusCode === '0') {
						res.body.data.addresses.forEach(function(val, index) {
							if (val.default) {
								that.addressData = val;
							}
						})
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				});
			},
			//查询秒杀活动
			inquiryGroupBuyById(groupBuyProductId) {
				let that = this;
				let postData = {
					groupBuyProductUuid: groupBuyProductId
				}
				this.$api.request.groupBuy(postData, res => {
					if (res.body.status.statusCode === '0') {
						this.groupbuy = res.body.data;
						this.product = res.body.data.productDTO;
						if(this.productSkuId){
							this.product.skuList.forEach(function(val,index){
								if(that.productSkuId === val.productSkuUuid){
									that.productSku = val;
								}
							})
						}
						that.productAmount = (that.groupbuy.unitPrice * this.unit).toFixed(2);
						this.actualAmount = this.toActualAmount(this.productAmount,this.deductAmount,this.freightAmount);
					} else {
						console.log(res.body.status.errorDesc);
					}
				});
			},
			// 计算运费金额
			calculateFreightAmount(groupBuyProductId, province, city, area) {
				let that = this;
				let postData = {
					groupBuyProduct:{
						groupBuyProductUuid: groupBuyProductId
					},
					orderDTO:{
						deliveryProvince: province,
						deliveryCity: city,
						deliveryArea: area,
						productUnit: this.unit
					}
				}
				this.$api.request.calculateGroupBuyFreightAmount(postData, res => {
					if (res.body.status.statusCode === '0') {
						that.freightAmount = res.body.data.orderDTO.freightAmount;
						that.actualAmount = that.toActualAmount(that.productAmount,that.deductAmount,that.freightAmount);
					} else {
						that.$api.msg(res.body.status.errorDesc);
					}
				}, false);
			},
			//计算总金额
			calcSelectedProductAmount(cartData) {
				var amount = 0;
				cartData.forEach(function(val, index) {
					if (val.productDTO.skuEnabled) {
						amount = amount + val.productSkuDTO.skuUnitPrice * val.unit;
					} else {
						amount = amount + val.productDTO.unitPrice * val.unit;
					}
				})
				return amount;
			},
			change(e) {
				console.log('是否打开:' + e.show)
			},
			togglePopup(type, open) {
				switch (type) {
					case 'top':
						this.content = '顶部弹出 popup'
						break

					case 'bottom':
						this.content = '底部弹出 popup'
						break
					case 'center':
						this.content = '居中弹出 popup'
						break
				}
				this.type = type
				this.$nextTick(() => {
					this.$refs['show' + open].open()
				})
			},
			submit() {
				//检查收货地址是否选择
				if (this.tabCurrentIndex==0&&!this.addressData.userDeliveryAddressUuid) {
					this.$api.msg('未选择收货地址');
					return;
				}
				if (this.tabCurrentIndex==1&&!this.merchantData.merchantUuid) {
					this.$api.msg('未选择提货门店');
					return;
				}
				//post订单数据到后台
				let postData = {
					groupBuyUuid: this.groupBuyId,
					groupBuyProduct: {
						groupBuyProductUuid: this.groupBuyProductId
					},
					orderDTO: {
						userDTO: {
							userUuid: this.userInfo.userUuid
						},
						deliveryAddressDTO: {
							userDeliveryAddressUuid: this.addressData.userDeliveryAddressUuid
						},
						merchantDTO:{
							merchantUuid: this.merchantData.merchantUuid
						},
						productUnit: this.unit,
						deliveryType: this.tabCurrentIndex==0?'1':'2'
					},
					productDTO: {
						productUuid: this.productId
					},
					productSkuDTO: {
						productSkuUuid: this.productSkuId
					},
					owner: (this.groupBuyId!=null&&this.groupBuyId!=''&&this.groupBuyId!='undefined')?false:true
				}
				this.$api.request.createGroupBuyOrder(postData, res => {
					if (res.body.status.statusCode === '0') {
						uni.redirectTo({
							url: '/pages/money/pay?orderNo=' + res.body.data.orderNo
						})
					} else {
						this.$api.msg(res.body.status.errorDesc);
					}
				});

			},
			stopPrevent() {}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}

	.address-section {
		padding: 30upx 0;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}

		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}

		.icon-you {
			font-size: 32upx;
			color: $font-color-light;
			margin-right: 30upx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}

	.goods-section {
		margin-top: 16upx;
		background: #fff;
		padding-bottom: 1px;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			display: flex;
			margin: 20upx 30upx;

			image {
				flex-shrink: 0;
				display: block;
				width: 140upx;
				height: 140upx;
				border-radius: 4upx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {
				font-size: 30upx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26upx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32upx;
				color: $font-color-dark;
				padding-top: 10upx;

				.price {
					margin-bottom: 4upx;
				}

				.number {
					font-size: 26upx;
					color: $font-color-base;
					margin-left: 20upx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		margin-top: 16upx;
		background: #fff;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {
			flex: 1;
			font-size: 26upx;
			color: $font-color-light;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 26upx;
			color: $font-color-light;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}

			& .close {
				color: $font-color-light;
				margin-left: 20upx;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	/* 支付列表 */
	.pay-list {
		padding-left: 40upx;
		margin-top: 16upx;
		background: #fff;

		.pay-item {
			display: flex;
			align-items: center;
			padding-right: 20upx;
			line-height: 1;
			height: 110upx;
			position: relative;
		}

		.icon-weixinzhifu {
			width: 80upx;
			font-size: 40upx;
			color: #6BCC03;
		}

		.icon-alipay {
			width: 80upx;
			font-size: 40upx;
			color: #06B4FD;
		}

		.icon-xuanzhong2 {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60upx;
			height: 60upx;
			font-size: 40upx;
			color: $base-color;
		}

		.tit {
			font-size: 32upx;
			color: $font-color-dark;
			flex: 1;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);

		.price-content {
			padding-left: 30upx;
		}

		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}

		.price {
			font-size: 36upx;
			color: $base-color;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
	}

	/* 优惠券面板 */
	.mask {
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 9995;
		transition: .3s;

		.mask-content {
			width: 100%;
			min-height: 30vh;
			max-height: 70vh;
			background: #f3f3f3;
			transform: translateY(100%);
			transition: .3s;
			overflow-y: scroll;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.mask-content {
				transform: translateY(0);
			}
		}
	}

	/* 优惠券列表 */
	.coupon-item {
		display: flex;
		flex-direction: column;
		margin: 20upx 24upx;
		background: #fff;

		.con {
			display: flex;
			align-items: center;
			position: relative;
			height: 120upx;
			padding: 0 30upx;

			&:after {
				position: absolute;
				left: 0;
				bottom: 0;
				content: '';
				width: 100%;
				height: 0;
				border-bottom: 1px dashed #f3f3f3;
				transform: scaleY(50%);
			}
		}

		.left {
			display: flex;
			flex-direction: column;
			justify-content: center;
			flex: 1;
			overflow: hidden;
			height: 100upx;
		}

		.title {
			font-size: 32upx;
			color: $font-color-dark;
			margin-bottom: 10upx;
		}

		.time {
			font-size: 24upx;
			color: $font-color-light;
		}

		.right {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 26upx;
			color: $font-color-base;
			height: 100upx;
		}

		.price {
			font-size: 44upx;
			color: $base-color;

			&:before {
				content: '￥';
				font-size: 34upx;
			}
		}

		.tips {
			font-size: 24upx;
			color: $font-color-light;
			line-height: 60upx;
			padding-left: 30upx;
		}

		.circle {
			position: absolute;
			left: -6upx;
			bottom: -10upx;
			z-index: 10;
			width: 20upx;
			height: 20upx;
			background: #f3f3f3;
			border-radius: 100px;

			&.r {
				left: auto;
				right: -6upx;
			}
		}
	}

	.coupon-list {
		height: 400px;
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		border-radius: 20upx;
		margin-bottom: 90upx;

		.coupon-list_item {
			width: 100%;
			height: 110px;
			margin: 10upx 0;
			background-color: #fff;
			border-radius: 5px;

			.coupon-left {
				float: left;
				width: 45%;
				height: 100%;
				padding: 10upx;

				image {
					width: 100%;
					height: 100%;
					opacity: 1;
				}
			}

			.coupon-right {
				position: relative;
				padding: 10upx;
				width: 55%;
				float: left;
				border-radius: 0px 10px 10px 0px;

				.name {
					color: $font-color-dark;
					font-size: $font-lg;
				}

				.valid {
					color: $font-color-light;
					font-size: $font-base;
					line-height: 40upx;
				}

				.stock {
					color: $font-color-light;
					font-size: $font-base;
					line-height: 40upx;
					margin-top: 10upx;
				}

				image {
					position: absolute;
					top: 10upx;
					right: 10upx;
					height: 100upx;
					width: 100upx;
				}
			}
		}
	}
	.navbar {
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0, 0, 0, .06);
		/* position: relative; */
		z-index: 10;
	
		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;
	
			&.current {
				color: $base-color;
	
				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}
	}
</style>
